package com.xhsd.zlong.parseexcel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import java.util.Map;

import com.xhsd.core.utils.StringKit;

public class Main {

	public static void main(String[] args) throws FileNotFoundException,
			IOException {
		List<Map<Integer, String>> rows = ExcelUtils.parse("d:\\sa.xls");
		
		//重定向输出
		File file = new File("d:\\sa.out");
		if(file.exists())file.delete();
		file = new File("d:\\sa.out");
		PrintStream out = new PrintStream(file);

		String pp = "", pc = "", pz = "";

		for (Map<Integer, String> map : rows) {

			String province = map.get(0);
			String city = map.get(1);
			String zone = map.get(2);

			if (!pp.equals(province)) {
				//
				pp = province;
				pc = city;
				pz = zone;
				
				//结束标签
				if (StringKit.notBlank(pz)) {
					out.println("}"); // 区end
					out.println("]}"); // 市end
				}else{
					out.println("}"); // 市end
				}
				out.println("]},"); // 省end
				
				//开始标签
				out.println("{\"n\":\"" + province + "\",\"s\":["); // 省start
				if (StringKit.notBlank(zone)) {
					out.println("{\"n\":\"" + city + "\",\"s\":["); // 市start
					out.println("{\"n\":\"" + zone + "\""); // 区start
				} else {
					out.println("{\"n\":\"" + city + "\""); // 市start
				}
			} else {
				if (!pc.equals(city)) {
					pc = city;
					pz = zone;

					//结束标签
					if (StringKit.notBlank(pz)) {
						out.println("}"); // 区
						out.println("]},"); // 市
					} else {
						out.println("},"); // 市
					}
					
					//开始标签
					if (StringKit.notBlank(zone)) {
						out.println("{\"n\":\"" + city + "\",\"s\":["); // 市start
						out.println("{\"n\":\"" + zone + "\""); // 区start
					} else {
						out.println("{\"n\":\"" + city + "\""); // 市start
					}
				} else {
					pz = zone;
					
					//结束标签
					if (StringKit.notBlank(pz)) {
						out.println("},"); // 区end
					}
					
					//开始标签
					if (StringKit.notBlank(zone)) {
						out.println("{\"n\":\"" + zone + "\""); // 区start
					}
				}
			}
		}

	}

}
